package com.bacel.nb.product.web.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.util.Properties;

public class RedisPoolUtils {

    private static JedisPool pool;

    static {
        try {
            Properties props = new Properties();
            props.load(RedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties"));
            //创建jedis池配置实例
            JedisPoolConfig config = new JedisPoolConfig();
            //设置池配置项值

            config.setMaxTotal(Integer.valueOf(props.getProperty("jedis.pool.maxTotal")));
            config.setMaxIdle(Integer.valueOf(props.getProperty("jedis.pool.maxIdle")));
            config.setMaxWaitMillis(Long.valueOf(props.getProperty("jedis.pool.maxWaitMillis")));
            config.setTestOnBorrow(Boolean.valueOf(props.getProperty("jedis.pool.testOnBorrow")));
            config.setTestOnReturn(Boolean.valueOf(props.getProperty("jedis.pool.testOnReturn")));
            //根据配置实例化jedis池
            pool = new JedisPool(config, props.getProperty("redis.ip"), Integer.valueOf(props.getProperty("redis.port")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Jedis getJedisObject(){
        return pool.getResource();
    }

    public static void recycleJedisOjbect(Jedis jedis){
        //pool.returnResource(jedis);
        pool.close();
    }
}
